﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using LocalService = Sunboxsoft.ASMS.Client.Domain.LocalService;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
using Sunboxsoft.ASMS.Client.Domain;


namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    public partial class MasterDataItemPicker : UserControl, IView
    {
        #region fields
        private int _pageIndex;
        private int _pageCount;
        private string _mdCode;
        private string _parentCode;
        private int? _tag;
        #endregion

        public MasterDataItemPicker(string masterDataCode, string parentCode, int? tag)
        {
            InitializeComponent();

            _mdCode = masterDataCode;
            _parentCode = parentCode;
            _tag = tag;
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("ok", "确定", OnOKActionClick))
                .AddAction(new ActionNode("cancel", "取消", OnCancelActionClick));
        }

        public void Active(ViewActiveArgs args)
        {
            LoadMasterDataItemListData();
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }

        #endregion

        #region actions
        private void OnOKActionClick(object sender, object e)
        {
            WebRef.EMasterDataItem item = ctlGridView.GetRow(ctlGridView.FocusedRowHandle) as WebRef.EMasterDataItem;
            if (item == null)
            {
                return;
            }
            UIContext.Current.ViewManager.CloseView(true, item);
        }

        private void OnCancelActionClick(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }

        private void ctlBtnQuery_Click(object sender, EventArgs e)
        {
            LoadMasterDataItemListData();
        }
        #endregion

        #region methods

        private void LoadMasterDataItemListData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                string filter = ctlEditFilter.Text.Trim();
                ListResult<WebRef.EMasterDataItem> result = new LocalService.QueryService().QueryMasterDataItemList(_mdCode, _parentCode, _tag);
                this.BeginInvoke(new Action<ListResult<WebRef.EMasterDataItem>>((p) =>
                {
                    if (p.IsSuccess)
                    {
                        //binding
                        if (string.IsNullOrEmpty(filter))
                        {
                            ctlGrid.DataSource = p.Data.Items;
                        }
                        else
                        {
                            ctlGrid.DataSource = p.Data.Items.Where(i => i.Code.Contains(filter) || i.Name.Contains(filter)).ToList();
                        }
                        //Common.GridEmbeddedNavigatorUtil.Update<WebRef.VBookingBill>(ctlGrid.EmbeddedNavigator, p, out _pageIndex, out _pageCount);
                    }
                    else
                    {
                        MessageBox.Show(result.Message);
                    }
                    this.HideMask();
                }), result);
            });
        }

        private int GetPageLimit()
        {
            return 15;
        }

        #endregion
    }
}
